package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;
import com.semanticweb.webapp.search.SearchMD;

/**
 * Class which represents 'Materijal i tehnika' from the chart.
 * 
 * @author Lab
 * 
 */
public class MaterialAndTechnique extends AbstractEntity implements IDocument {

    public static final String ONT_MATERIAL = "material";
    public static final String ONT_TECHNIQUE = "technique";
    public static final String ONT_PRODUCTION = "production";

    private String material;
    private String technique;

    public MaterialAndTechnique(final String id) {
        super(id);
    }

    public MaterialAndTechnique(final String id, final String material, final String technique) {
        super(id);
        this.material = material;
        this.technique = technique;
    }

    @Override
    public void toRDF() {
        final String itemURI = Item.formatItemURI(this.id);
        final Query query = new Query();
        String[] result;

        // material
        if (this.material != null) {
            String materialURI;
            result = query.select("material").type("material", CIDOC.E57.queryValue()).label("material", this.material)
                    .find();
            if (result != null && result.length > 0) {
                materialURI = result[0];
            } else {
                materialURI = Ont.formatURI(this.id, ONT_MATERIAL);

                Ont.getOntology().createIndividual(CIDOC.E57.getFullValue(), materialURI);
                Ont.getOntology().addLabel(materialURI, this.material);
            }

            Ont.getOntology().connectIndividuals(itemURI, CIDOC.P45.getFullValue(), materialURI);
        }

        // technique
        if (this.technique != null) {
            String productionURI;
            result = query.select("technique").type("production", CIDOC.E12.queryValue())
                    .type("technique", CIDOC.E55.queryValue()).label("technique", this.technique)
                    .connect("production", CIDOC.P32.queryValue(), "technique").find();
            if (result == null || result.length == 0) {
                final String techniqueURI = Ont.formatURI(this.id, ONT_TECHNIQUE);
                productionURI = Ont.formatURI(this.id, ONT_PRODUCTION, ONT_TECHNIQUE);

                Ont.getOntology().createIndividual(CIDOC.E12.getFullValue(), productionURI);
                Ont.getOntology().createIndividual(CIDOC.E55.getFullValue(), techniqueURI);

                Ont.getOntology().addLabel(techniqueURI, this.technique);

                Ont.getOntology().connectIndividuals(productionURI, CIDOC.P32.getFullValue(), techniqueURI);
            } else {
                productionURI = result[0];
            }

            Ont.getOntology().connectIndividuals(productionURI, CIDOC.P108.getFullValue(), itemURI);
        }
    }

    public void fromRDF() {
        final String itemQueryURI = Item.formatItemQueryURI(this.id);
        final Query q = new Query();

        // material
        String[] result = q.selectLabel().connect(itemQueryURI, CIDOC.P45.queryValue(), "material")
                .type("material", CIDOC.E57.queryValue()).findLabel("material");
        if (result != null && result.length > 0) {
            this.material = result[0];
        }

        // technique
        result = q.selectLabel().connect("production", CIDOC.P108.queryValue(), itemQueryURI)
                .type("production", CIDOC.E12.queryValue()).type("technique", CIDOC.E55.queryValue())
                .connect("production", CIDOC.P32.queryValue(), "technique").findLabel("technique");
        if (result != null && result.length > 0) {
            this.technique = result[0];
        }
    }

    public String getMaterial() {
        return this.material;
    }

    public void setMaterial(final String material) {
        this.material = material;
    }

    public String getTechnique() {
        return this.technique;
    }

    public void setTechnique(final String technique) {
        this.technique = technique;
    }

    @Override
    public void toIndexDocument(final Document doc) {
        if (this.material != null) {
            doc.add(SearchMD.MATERIAL.createField(this.material));
            doc.add(SearchMD.CONTENT.createField(this.material));
        }
        if (this.technique != null) {
            doc.add(SearchMD.TECHNIQUE.createField(this.technique));
            doc.add(SearchMD.CONTENT.createField(this.technique));
        }
    }

}
